package com.fancon.rage.comic.json.rpc;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.arnx.jsonic.JSONException;

import com.fancon.rage.comic.dao.RagePhotoDao;
import com.fancon.rage.comic.entity.Photo;
import com.fancon.rage.comic.entity.PhotoJson;

@SuppressWarnings("serial")
public class RageRpcJsonSevletEn extends RageBaseService{
	protected RagePhotoDao dao = new RagePhotoDao();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		resp.setHeader("Content-Encoding", "utf-8");
    	String action = req.getParameter("action");
        if ("json".equals(action)) {           
            int page = Integer.parseInt(req.getParameter("page").replace("_", "")); // WTF?            
            showPage(page, req, resp);
        } else if("all".equals(action)){
            showAll(req, resp);
        }else if("numpage".equals(action)){
        	
        }
        
	}
	private void showPage(int page, HttpServletRequest req, HttpServletResponse resp){
		
	}
	private void showAll(HttpServletRequest req, HttpServletResponse resp){
		List<Photo> enComics = dao.listByProperty("countryCode", "en", 1000, 0);
		ArrayList<PhotoJson> enJsons = new ArrayList<PhotoJson>();
		for (Photo photo : enComics) {
			enJsons.add(photo.toJson(1));
		}
		PrintWriter out = null;
		try {
			out = resp.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			out.print(encode(enJsons));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void numPage(HttpServletRequest req, HttpServletResponse resp){
		
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}
}
